package game.engine;
import org.lwjgl.opengl.GL11;

import physics.shapes.Polygon;

import tools.Floatmath;
import tools.Point;
import tools.intersectionTests.IntersectionTest;
import graphics.objects.GraphicsObject;
import graphics.system.Color;
import graphics.system.GLTools;
public class TriggerArea extends GraphicsObject {
	private Point center;
	private float width;
	private float height;
	public TriggerArea(Point p0, Point p1) {
		super(p0.add(p1).mul(0.5f), 0, Color.red);
		node.boundingrad = Floatmath.max(node.pos.distance(p0), node.pos.distance(p1));
		width = Math.abs(p0.x - p1.x);
		height = Math.abs(p0.y - p1.y);
	}
	public Point getCenter() {
		return node.pos;
	}
	public boolean isIn(Point p) {
		Point pts[] = getPointsTransformed();
		/*
		 * Point p0 = new Point(pts[0]); pts[0] = pts[1].sub(pts[0]); pts[1] = pts[2].sub(pts[1]); pts[2] = pts[3].sub(pts[2]); pts[3] = p0.sub(pts[3]);
		 */
		return IntersectionTest.intersects(p.sub(node.pos), pts);
	}
	private Point[] getPointsTransformed() {
		Point p[] = {new Point(-width / 2, -height / 2), new Point(width / 2, -height / 2), new Point(width / 2, height / 2), new Point(-width / 2, height / 2)};
		for (Point c : p) {
			c = c.rotate(node.apos);
			c.x *= node.scale.x;
			c.y *= node.scale.y;
			c.sAdd(node.pos);
		}
		return p;
	}
	public void drawDebug() {
		GL11.glPushMatrix();
		node.setUpGl();
		GL11.glEnable(GL11.GL_LINE_STIPPLE);
		GL11.glLineStipple(1, (short) 0xAAAA);
		Point p[] = getPointsTransformed();
		GLTools.drawLineLoop(p, Color.red);
		GL11.glDisable(GL11.GL_LINE_STIPPLE);
		GL11.glPopMatrix();
	}
	public String toFileString() {
		String s = super.toFileString();
		s = s + "/" + width + "/" + height;
		return s;
	}
	public int fromFileString(String s) {
		int off = super.fromFileString(s);
		String l[] = s.split("/");
		this.width = Float.valueOf(l[off + 1]);
		this.height = Float.valueOf(l[off + 2]);
		node.boundingrad = Floatmath.max(width, height);
		return off + 2;
	}
}
